package com.example.front.serivce;

import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.ButtonSearchDto;
import com.example.front.dto.EventDto;
import com.example.front.dto.EventSearchDto;
import com.example.front.entity.Event;
import com.example.front.entity.Level;
import com.example.front.mapper.EventMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class EventService {
    @Resource
    EventMapper eventMapper;
    @Resource
    ButtonService buttonService;

    /**
     * 添加按钮事件
     */
    @Transactional
    public void createEvent(Event event) {
        //查重
        if (eventMapper.findByName(event) != null) {
            throw new BusinessException("事件名称已存在");
        }
        //添加
        if (eventMapper.create(event) == 0) {
            throw new BusinessException("操作异常");
        }
    }

    /**
     * 删除
     */
    @Transactional
    public void deleteEvent(Event event) {
        if (buttonService.findByEventId(event.getTenantId(), event.getId()) != 0) {
            throw new BusinessException("该事件绑定了按钮，请先删除或修改那些按钮事件再执行该操作!");
        }
        if (eventMapper.delete(event) == 0) {
            throw new BusinessException("事件不存在或操作异常");
        }
    }

    /**
     * 修改按钮事件
     */
    @Transactional
    public void updateEvent(Event event) {
        //查看修改过后的名称是否和其他名称重复
        List<Level> list = new ArrayList<>();
        if (!Objects.equals(eventMapper.findAll(event, new Pagination()), list)) {
            throw new BusinessException("名称已存在");
        }
        if (eventMapper.update(event) == 0) {
            throw new BusinessException("操作失败");
        }
    }

    /**
     * 列出事件列表
     */
    public ListDto<EventDto> listEvent(EventSearchDto condition, Pagination pagination) {
        long total = eventMapper.count(condition);
        if (total <= 0) {
            return new ListDto<>(new ArrayList<>(), pagination);
        }
        pagination.setTotal(total);
        Event event = new Event();
        BeanUtils.copyProperties(condition, event);
        List<Event> events = eventMapper.findAll(event, pagination);
        List<EventDto> eventDtoList = events.stream().map(event1 -> {
            EventDto eventDto = new EventDto();
            BeanUtils.copyProperties(event1, eventDto);
            return eventDto;
        }).collect(Collectors.toList());
        return new ListDto<>(eventDtoList, pagination);
    }

    /**
     * 检查event是否存在
     */
    public String findById(Long tenantId, Long id) {
        Event event = eventMapper.findById(tenantId, id);
        if (event == null) {
            return "该事件已被删除";
        } else {
            return event.getName();
        }
    }

}
